# config_util.py
import configparser
import os


class ConfigUtil:
    DEFAULT_CONFIG = {
        'server': {
            'base_url': 'http://localhost:90'
        }
    }

    def __init__(self, config_file='config.ini'):
        self.config_file = config_file
        self.config = configparser.ConfigParser()

        if not os.path.exists(self.config_file):
            self._create_default_config()
        else:
            self.config.read(self.config_file, encoding='utf-8')

    def _create_default_config(self):
        """创建默认配置文件"""
        for section, values in self.DEFAULT_CONFIG.items():
            self.config[section] = values
        with open(self.config_file, 'w', encoding='utf-8') as f:
            self.config.write(f)
        print(f"已生成默认配置文件 {self.config_file}")

    def get_base_url(self):
        return self.config.get('server', 'base_url')
